﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace CarBloke.Pages
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        Boolean initialSetup = true;

        public SettingsPage()
        {
            InitializeComponent();

            mpgUSRadioButton.Tag = ApplicationSettings.EfficiencyUnits.MilesPerGallon;
            mpgUKRadioButton.Tag = ApplicationSettings.EfficiencyUnits.MilesPerImperialGallon;
            kmlRadioButton.Tag = ApplicationSettings.EfficiencyUnits.KilometersPerLiter;
            l100kmRadioButton.Tag = ApplicationSettings.EfficiencyUnits.LiterPer100Kilometers;

            milesRadioButton.Tag = ApplicationSettings.DistanceUnits.Miles;
            kilometersRadioButton.Tag = ApplicationSettings.DistanceUnits.Kilometers;

            gallonsUSRadioButton.Tag = ApplicationSettings.VolumeUnits.Gallon;
            gallonsUKRadioButton.Tag = ApplicationSettings.VolumeUnits.ImperialGallon;
            litersRadioButton.Tag = ApplicationSettings.VolumeUnits.Liter;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            initalizeUserInterface();
        }

        private void initalizeUserInterface()
        {
            switch (ApplicationSettings.Current.DistanceUnit)
            {
                case ApplicationSettings.DistanceUnits.Kilometers:
                    kilometersRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.DistanceUnits.Miles:
                    milesRadioButton.IsChecked = true;
                    break;
            }

            switch (ApplicationSettings.Current.VolumeUnit)
            {
                case ApplicationSettings.VolumeUnits.Gallon:
                    gallonsUSRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.VolumeUnits.ImperialGallon:
                    gallonsUKRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.VolumeUnits.Liter:
                    litersRadioButton.IsChecked = true;
                    break;
            }

            switch (ApplicationSettings.Current.EfficiencyUnit)
            {
                case ApplicationSettings.EfficiencyUnits.MilesPerGallon:
                    mpgUSRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.EfficiencyUnits.MilesPerImperialGallon:
                    mpgUKRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.EfficiencyUnits.KilometersPerLiter:
                    kmlRadioButton.IsChecked = true;
                    break;
                case ApplicationSettings.EfficiencyUnits.LiterPer100Kilometers:
                    l100kmRadioButton.IsChecked = true;
                    break;
            }

            initialSetup = false;
        }

        private void efficiencyRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!initialSetup)
            {
                ApplicationSettings.EfficiencyUnits selectedUnit = (ApplicationSettings.EfficiencyUnits)(sender as RadioButton).Tag;
                ApplicationSettings.Current.EfficiencyUnit = selectedUnit;
            }
        }

        private void fuelRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!initialSetup)
            {
                ApplicationSettings.VolumeUnits selectedUnit = (ApplicationSettings.VolumeUnits)(sender as RadioButton).Tag;
                ApplicationSettings.Current.VolumeUnit = selectedUnit;
            }
        }

        private void odometerRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!initialSetup)
            {
                ApplicationSettings.DistanceUnits selectedUnit = (ApplicationSettings.DistanceUnits)(sender as RadioButton).Tag;
                ApplicationSettings.Current.DistanceUnit = selectedUnit;
            }
        }
    }
}